import os
import sys
import time
import subprocess

sys.path.append(os.path.join(os.getcwd()))
import services
from services import *

PLUGIN_FOLDER = "webui"
PLUGIN_FOLDER_PATH = os.path.join(os.path.dirname(__file__),PLUGIN_FOLDER)
MANAGE_PY_PATH = os.path.join(PLUGIN_FOLDER_PATH,"manage.py")
LAUNCH_COMMAND = "{0} {1} {2}".format("python",MANAGE_PY_PATH,"runserver")

class WebUIService(BasicService):
    def __init__(self, web_ui_pid, debug):
        BasicService.__init__(self)
        self.web_ui_pid = web_ui_pid

    def stop(self):
        os.kill(self.web_ui_pid, 9)
        os.kill(self.web_ui_pid+1, 9)
        BasicService.stop(self)

if __name__ == "__main__":
    web_ui = subprocess.Popen(LAUNCH_COMMAND,shell=True)
    print "WEB UI PID : ", web_ui.pid
    web_ui_service = WebUIService(web_ui.pid, True)
    services.launch_service(web_ui_service, "webui")
